package com.tools.compass;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;
import com.tools.compass.util.LogI;

public class CompassApp extends Application {

	static CompassApp mApp;

	// 百度MapAPI的管理类
	BMapManager mBMapMan = null;

	// 授权Key
	// 请输入您的Key,
	// 申请地址：http://dev.baidu.com/wiki/static/imap/key/
	String mStrKey = "7E555C5993DED9AB26FEB1AD3C151093AAE51F7F";
	boolean m_bKeyRight = true; // 授权Key正确，验证通过

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	static class MyGeneralListener implements MKGeneralListener {
		@Override
		public void onGetNetworkState(int iError) {
			LogI.d("MyGeneralListener", "onGetNetworkState error is " + iError);
			Toast.makeText(CompassApp.mApp.getApplicationContext(),
					R.string.app_name, Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onGetPermissionState(int iError) {
			Log.d("MyGeneralListener", "onGetPermissionState error is "
					+ iError);
			if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
				// 授权Key错误：
				Toast.makeText(CompassApp.mApp.getApplicationContext(),
						R.string.bmap_key_err, Toast.LENGTH_SHORT).show();
				CompassApp.mApp.m_bKeyRight = false;
			}
		}
	}

	@Override
	public void onCreate() {
		LogI.v("BMapApiDemoApp:onCreate");
		mApp = this;
		mBMapMan = new BMapManager(this);
		boolean isSuccess = mBMapMan
				.init(this.mStrKey, new MyGeneralListener());
		// 初始化地图sdk成功，设置定位监听时间
		if (isSuccess) {
			mBMapMan.getLocationManager().setNotifyInternal(10, 5);
		} else {
			// 地图sdk初始化失败，不能使用sdk
		}

		super.onCreate();
	}

	// 建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
	@Override
	public void onTerminate() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onTerminate();
	}

}
